package config;

import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.spec.HttpServletRequestImpl;
import io.undertow.util.HeaderMap;
import io.undertow.util.HttpString;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一判断header是否有userId 信息  如果没有表示是后端定时过来的请求添加固定参数
 *
 * @author shaoyn
 */
@Configuration
public class FeignConfiguration implements RequestInterceptor {

    public static final org.slf4j.Logger log = LoggerFactory.getLogger(FeignConfiguration.class);

    public static final String RE_SUB_TOKEN = "X-RESUB-TOKEN";
    private static final String JTPF_USER_ID = "jtpf.userId";
    private static final String JTPF_SYS_CODE = "jtpf.sysCode";
    private static final String AUTHORIZATION = "Authorization";
    private static final String DEFUALT_USER_ID = "00000000";
    @Value("${remote.authorization}")
    private String localAuthorization;

    /**
     * 设置网关token
     *
     * @param template
     * @param localAuthorization
     */
    public static void apply(RequestTemplate template, String localAuthorization) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null == attributes || null == attributes.getRequest()) {
            //定时起的任务
            template.header(JTPF_USER_ID, DEFUALT_USER_ID);
            //由于本地测试时走网关服务所以这里需要添加本地调用适配
            if (StringUtils.isNotBlank(localAuthorization)) {
                template.header(AUTHORIZATION, "Bearer " + localAuthorization);
            }
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        String userId = request.getHeader(JTPF_USER_ID);
        String sysCode = request.getHeader(JTPF_SYS_CODE);
        String authorization = request.getHeader(AUTHORIZATION);
        String reSubToken = request.getHeader(RE_SUB_TOKEN);
        template.header(RE_SUB_TOKEN, reSubToken);

        //添加token
        template.header(AUTHORIZATION, authorization);
        if (StringUtils.isNotBlank(localAuthorization)) {
            template.header(AUTHORIZATION, "Bearer " + localAuthorization);
        }
        if (StringUtils.isNotBlank(userId)) {
            //正常的通过服务间调用过来的请求
            template.header(JTPF_USER_ID, userId);
            template.header(JTPF_SYS_CODE, sysCode);
        }

    }

    public static void addHeader(String headerName, String value) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return;
        }
        HttpServletRequestImpl httpRequest = (HttpServletRequestImpl) attributes.getRequest();
        HttpServerExchange exchange = httpRequest.getExchange();
        HeaderMap headerMap = exchange.getRequestHeaders();
        if (StringUtils.isNotBlank(headerName)) {
//            headerMap.add(new HttpString(headerName), value);
            headerMap.put(new HttpString(headerName), value);
        }
    }

    @Override
    public void apply(RequestTemplate template) {
        apply(template, localAuthorization);
    }

    @Bean
    public Logger.Level logger() {
        return Logger.Level.FULL;
    }
}


